package com.keyrus.order.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.keyrus.order.pojo.KomsResult;
import com.keyrus.order.pojo.Order;
import com.keyrus.order.service.KomsOrderService;


/**
 * 订单Controller
 *
 * @author David
 * @create 2018-10-23 23:27
 */
@RestController
@RefreshScope
public class KomsOrderController
{
    private static final Logger LOG = LogManager.getLogger(KomsOrderController.class);

    @Resource
    private KomsOrderService komsOrderService;

    /**
     *
     * @param code  订单号
     * @param limit     展示条目数
     * @param page      分页
     * @param platform      订单所属平台，可传多个
     * @param status        订单状态
     * @return      成功失败
     */
    @RequestMapping(value = "/get")
    @ResponseBody
    public KomsResult showOrders(@RequestParam(required = false) String code, @RequestParam(required = false) Integer limit,
                                 @RequestParam(required = false) Integer page,
                                 @RequestParam(required = false, value = "platform[]") List<String> platform,
                                 @RequestParam(required = false, value = "status[]") List<String> status)
    {
        if (limit == null)
        {
            limit = 10;
        }
        if (page == null)
        {
            page = 1;
        }

        if (StringUtils.isBlank(code))
        {
            return komsOrderService.getAllOrders(platform, status, new PageRequest(page - 1, limit));
        }
        else
        {
            return komsOrderService.getOrderByCode(code, platform, status);
        }
    }

    /**
     * 根据传入json创建订单
     * @param orderDTO   订单DTO
     * @return  成功失败
     */
    @PostMapping(value = "/create")
    @ResponseBody
    public KomsResult generateOrder(@RequestBody Order orderDTO)
    {
        return komsOrderService.generateOrder(orderDTO);
    }

}
